package cn.com.nes.site.module.dao;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Repository
public class InstallDaoImpl extends BaseDaoImpl {
	public PageData getAgentCompanyList(DataReq req) {
		PageData res = new PageData();
		StringBuffer sb = new StringBuffer();
		try {
			
			//厂商
			
			sb.delete(0, sb.length()) ;
			if("01".equals(req.getUinfo().getCompanyType())) {
				sb.append("SELECT companyId, companyName,nes_companyId manufacture_id from t_company where companyId = ?  order by companyName ") ;
			}else if("02".equals(req.getUinfo().getCompanyType())) {//代理商
				sb.append("SELECT a.companyId, a.companyName,a.nes_companyId manufacture_id from t_company a INNER JOIN t_agent_facturer b on a.companyId=b.facturer_id where b.agent_id = ? and b.deleted='0' and a.nes_companyId <>''  and a.nes_companyId is not null   order by a.companyName  ");
			}else {
				sb.append("SELECT companyId, companyName,nes_companyId manufacture_id from t_company where companyType='01' and nes_companyId is not null and nes_companyId<>'' or '1'=? ") ;
			}
			List<Map<String, Object>> list = this.getJdbcTemplate_newg()
					.queryForList(sb.toString(),
							new Object[] {req.getUinfo().getCompanyId() });
			
	
			res.setPage(list);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			res.setRetCode(9);
			res.setRetMessage("操作失败！");
			e.printStackTrace();
		} finally {

		}
		return res;
	}
	
	public PageData getSblxList(DataReq req) {
		PageData res = new PageData();
		StringBuffer sb = new StringBuffer();
		Connection conn = null ;
		try {
			conn = DBUtils.getDBConn_Ope();
			//厂商
			String manufacture_id = (String) req.getReqData().get("manufacture_id") ;
			sb.append("SELECT  ") ;
			sb.append(" DISTINCT c.sblx  ") ;
			sb.append(", idvi.value_id AS sblxDicId  ") ;
			sb.append(", idvi.title AS sblxmc  ") ;
			sb.append("FROM  ") ;
			sb.append("t_sblx c  ") ;
			sb.append("INNER JOIN is_dict_value_info idvi ON  idvi.type_id = 'd_type_2' AND idvi.dic_no =c.sblx  ") ;
			sb.append("WHERE c.manufacture_id = ?  order by idvi.title desc ") ;
			List<Map<String, Object>> list = this.queryForList(conn,sb.toString(),
					new Object[] {manufacture_id});
			res.setPage(list);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			res.setRetCode(9);
			res.setRetMessage("操作失败！");
			e.printStackTrace();
		} finally {
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return res;
	}
	
	public PageData getSbxhList(DataReq req) {
		PageData res = new PageData();
		StringBuffer sb = new StringBuffer();
		Connection conn = null ;
		try {
			conn = DBUtils.getDBConn_Ope();
			//厂商
			String manufacture_id = (String) req.getReqData().get("manufacture_id") ;
			String sblx = (String) req.getReqData().get("sblx") ;
			sb.append("SELECT sblx_id,sbxh from t_sblx where manufacture_id = ? and sblx = ? order by sbxh ") ;
			List<Map<String, Object>> list = this.queryForList(conn,sb.toString(),
					new Object[] {manufacture_id,sblx});
			res.setPage(list);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			res.setRetCode(9);
			res.setRetMessage("操作失败！");
			e.printStackTrace();
		} finally {
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return res;
	}

	public PageData getWxsbList(DataReq req) {
		PageData res = new PageData();
		StringBuffer sb = new StringBuffer();
		Connection conn = null ;
		try {
			conn = DBUtils.getDBConn_Ope();
			//厂商
			String manufacture_id = (String) req.getReqData().get("manufacture_id") ;
			sb.append("SELECT wx_id from is_wxsb a where manufacture_id = ? and not EXISTS (SELECT 1 from is_equipment_info b where a.wx_id=b.wx_id and b.sfsc='0') ORDER BY a.wx_id ") ;
			List<Map<String, Object>> list = this.queryForList(conn,sb.toString(),
					new Object[] {manufacture_id});
			res.setPage(list);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			res.setRetCode(9);
			res.setRetMessage("操作失败！");
			e.printStackTrace();
		} finally {
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return res;
	}
	
	public PageData getSbList(DataReq req) {
		PageData res = new PageData();
		StringBuffer sb = new StringBuffer();
		Connection conn = null ;
		try {
			conn = DBUtils.getDBConn_Ope();
			//厂商
			String project_id = (String) req.getReqData().get("project_uuid") ;
			sb.append("SELECT a.xmlxr,a.xmlxrdh,a.xmdz,b.wx_id,b.alias,date_format(c.gmt_create,'%Y-%m-%d %T') gmt_create, date_format(b.cjsj,'%Y-%m-%d %T') cjsj from is_install_info a INNER JOIN is_equipment_info b on a.dd_id=b.dd_id LEFT JOIN is_norm_data c on b.sb_id=c.sb_id where a.dd_id = ?  ") ;
			List<Map<String, Object>> list = this.queryForList(conn,sb.toString(),
					new Object[] {project_id});
			res.setPage(list);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			res.setRetCode(9);
			res.setRetMessage("操作失败！");
			e.printStackTrace();
		} finally {
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return res;
	}
	
	
}